package cn.tedu.antconlonysever.contract.service.impl;

import cn.tedu.antcolonycommons.ex.ServiceException;
import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.web.ServiceCode;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IContractRepository;
import cn.tedu.antconlonysever.contract.dao.persist.repository.IOrderRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Order;
import cn.tedu.antconlonysever.contract.pojo.param.OrderAddNewParam;
import cn.tedu.antconlonysever.contract.pojo.param.OrderUpdateParam;
import cn.tedu.antconlonysever.contract.pojo.vo.*;
import cn.tedu.antconlonysever.contract.service.IOrderService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;


@Service
@Slf4j
public class OrderServiceImpl implements IOrderService {
    @Value("${antcolony.crud.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IOrderRepository orderRepository;
    @Autowired
    private IContractRepository contractRepository;
    @Override
    public void addNew(OrderAddNewParam orderAddNewParam) {
        log.debug("开始处理【添加订单】的业务，参数：{}", orderAddNewParam);
        Long cusId = orderAddNewParam.getCusId();
        int count = orderRepository.countCusId(cusId);
        log.debug("根据订单编号统计匹配的合同数量，结果：{}", count);
        if(count>0){
            String message = "添加订单失败，订单编号已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        // 将订单数据写入到数据库中
        Order order=new Order();
        BeanUtils.copyProperties(orderAddNewParam,order);

        order.setId(orderAddNewParam.getCusId());
        order.setOrderDate(LocalDateTime.now());
        order.setGmtCreate(LocalDateTime.now() );
        order.setGmtModified(LocalDateTime.now() );
        int rows = orderRepository.insert(order);
        if(rows!=1){
            String message = "添加订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        log.debug("将新的订单数据插入到数据库，完成！");
    }

    @Override
    public void delete(Long id) {
        log.debug("开始处理【删除订单】的业务，参数：{}", id);
        OrderStandardVO order = orderRepository.getStandardById(id);
        if(order==null){
            String message = "删除订单失败，订单编号不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        Integer countByOrderId = contractRepository.countOrderId(id);
        if(countByOrderId>0){
            String message = "删除订单失败，仍有合同关联此订单！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        int rows = orderRepository.deleteById(id);
        if(rows!=1){
            String message = "删除订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }
        log.debug("将数据从数据库删除完成！");
    }

    @Override
    public void updateInfoById(Long id, OrderUpdateParam orderUpdateParam) {
        log.debug("开始处理【修改订单】的业务，ID：{}，新数据：{}", id, orderUpdateParam);

        OrderStandardVO orderStandardVO = orderRepository.getStandardById(id);
        if(orderStandardVO==null){
            String message = "修改订单失败，订单数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }

        Integer count = orderRepository.countCusIdNotId(id,orderUpdateParam.getCusId());
        if(count>0){
            String message = "修改订单失败，尝试修改的订单编号已存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }
        Order order=new Order();
        BeanUtils.copyProperties(orderUpdateParam,order);
        order.setId(id);
        int rows = orderRepository.update(order);
        if(rows!=1){
            String message = "修改订单失败，服务器忙，请稍后再试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_UPDATE, message);        }

    }

    @Override
    public OrderStandardVO getStandardById(long id) {
        log.debug("开始处理【根据ID查询合同详情】的业务，ID：{}", id);
        OrderStandardVO queryResult = orderRepository.getStandardById(id);
        if(queryResult==null){
            String message = "根据ID查询合同详情失败，合同数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<OrderListVO> list(Integer pageNum) {
        PageData<OrderListVO> pageData = orderRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<OrderListVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询订单列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        PageData<OrderListVO> pageData = orderRepository.list(pageNum, pageSize);
        return pageData;
    }


}
